package com.clkj.sunhelp.mvp.publicityshow.publicityofapplyresult;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clkj.sunhelp.R;
import com.clkj.sunhelp.adapter.list.ListAdapterForPublicityOfApplyResult;
import com.clkj.sunhelp.app.CustomApplication;
import com.clkj.sunhelp.bean.PublicityOfApplyResultListItem;
import com.clkj.sunhelp.common.Constant;
import com.clkj.sunhelp.common.IntentKey;
import com.clkj.sunhelp.mvp.base.BaseActivity;
import com.clkj.sunhelp.util.ToastUtil;
import com.clkj.sunhelp.util.schedulers.SchedulerProvider;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 申请结果公示展示界面
 * Created by Administrator on 2017/3/27.
 */

public class ActivityPublicityOfApplyResultShowView extends BaseActivity implements PullToRefreshBase.OnRefreshListener2, PublicityOfApplyResultSearchContract.View {

    private PullToRefreshListView lv;
    private ListView originalLv;
    private RelativeLayout layoutEmpty;
    private TextView tvEmptytip;


    List<PublicityOfApplyResultListItem> mPublicityList = new ArrayList<>();
    ListAdapterForPublicityOfApplyResult mAdapter;


    PublicityOfApplyResultSearchContract.Presenter mPresenter;


    Integer mPageIndex;
    boolean isRefresh;
    boolean canLoad;


    String mProjKind;
    String mQuXian;
    String mJieDaoOrXiangZhen;
    String mCunLuoOrSheQu;
    String mIdCardNo;
    //审核状态
    String mExamStatus;
    //发放年度
    String mReleaseYear;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publicity_apply_result_show_view);
        initTitleWithRightTvOrIv(null, null, "审定结果公示", true, null, null);
        initData();
        assignView();
        initView();

        mPresenter = new PublicityOfApplyResultSearchPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());
        getOriginalList();
    }

    @Override
    public void initData() {

        mProjKind = getIntent().getStringExtra(IntentKey.INTENT_KEY_PROJ_KIND);
        mQuXian = getIntent().getStringExtra(IntentKey.INTENT_KEY_QUXIAN);
        mJieDaoOrXiangZhen = getIntent().getStringExtra(IntentKey.INTENT_KEY_XIANGZHEN_JIEDAO);
        mCunLuoOrSheQu = getIntent().getStringExtra(IntentKey.INTENT_KEY_CUNLUO_SHEQU);
        mIdCardNo = getIntent().getStringExtra(IntentKey.INTENT_KEY_IDCARD_NO);
        //审核状态
        mExamStatus = getIntent().getStringExtra(IntentKey.INTENT_KEY_EXAM_STATUS);
        //发放年度
        mReleaseYear = getIntent().getStringExtra(IntentKey.INTENT_KEY_RELEASE_YEAR);


        /*PublicityOfApplyResultItem publicityOfApplyResultItem = new PublicityOfApplyResultItem();
        publicityOfApplyResultItem.setIndexNo("1");
        publicityOfApplyResultItem.setHomeOwnerName("户主名");
        publicityOfApplyResultItem.setFamilyPersonNum("10");
        publicityOfApplyResultItem.setStartHasYearMonth("2017年3月");
        publicityOfApplyResultItem.setIsRelativeAssociate("否");
        publicityOfApplyResultItem.setIsNewIn("否");

        mPublicityList.add(publicityOfApplyResultItem);
        mPublicityList.add(publicityOfApplyResultItem);
        mPublicityList.add(publicityOfApplyResultItem);
        mPublicityList.add(publicityOfApplyResultItem);
        mPublicityList.add(publicityOfApplyResultItem);
        mPublicityList.add(publicityOfApplyResultItem);
        mPublicityList.add(publicityOfApplyResultItem);*/
    }

    @Override
    public void assignView() {
        lv = (PullToRefreshListView) findViewById(R.id.lv);
        originalLv = lv.getRefreshableView();
        layoutEmpty = (RelativeLayout) findViewById(R.id.layout_empty);
        tvEmptytip = (TextView) findViewById(R.id.tv_emptytip);

    }

    @Override
    public void initView() {

        lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        lv.setOnRefreshListener(this);

        mAdapter = new ListAdapterForPublicityOfApplyResult(this, mPublicityList);
        originalLv.setAdapter(mAdapter);

    }


    private void getOriginalList() {
        isRefresh = true;
        mPageIndex = 1;
        getPublicityOfApplyResultList();
    }

    private void getMoreList() {
        if (canLoad) {
            mPageIndex++;
            getPublicityOfApplyResultList();
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        getOriginalList();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        getMoreList();
    }

    @Override
    public void getPublicityOfApplyResultList() {
        mPresenter.getPublicityOfApplyResultList(
                TextUtils.isEmpty(mProjKind)==true?"":mProjKind,
                TextUtils.isEmpty(mReleaseYear)==true?"":mReleaseYear,
                TextUtils.isEmpty(mQuXian)==true?"":mQuXian,
                TextUtils.isEmpty(mJieDaoOrXiangZhen)==true?"":mJieDaoOrXiangZhen,
                TextUtils.isEmpty(mCunLuoOrSheQu)==true?"":mCunLuoOrSheQu,
                TextUtils.isEmpty(mIdCardNo)==true?"":mIdCardNo,
                TextUtils.isEmpty(mExamStatus)==true?"":mExamStatus,
                mPageIndex.toString(),
                Constant.DEFAULT_PAGE_SIZE.toString());
    }

    @Override
    public void onGetPublicityOfApplyResultListSuccess(List<PublicityOfApplyResultListItem> publicityList) {

        lv.onRefreshComplete();

        if (isRefresh)
        {
           mPublicityList.clear();
           isRefresh = false;
        }
        if (publicityList.size()>0)
        {
           if (publicityList.size()>=Constant.DEFAULT_PAGE_SIZE)
           {
              lv.setMode(PullToRefreshBase.Mode.BOTH);
              canLoad = true;
           }else{
              canLoad = false;
              lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
           }
            mPublicityList.addAll(publicityList);

        }else{
              canLoad = false;
              lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

              if (mPublicityList.size()==0)
              {
                lv.setEmptyView(layoutEmpty);
              }else{
                ToastUtil.show(this, "已经获取全部数据", 2000);
                    }
              }

              mAdapter.notifyDataSetChanged();

    }

    @Override
    public void onGetPublicityOfApplyResultListError(String tip) {
        lv.onRefreshComplete();
        ToastUtil.showShort(this, tip);
        if (isRefresh) {
            mPublicityList.clear();
            lv.setEmptyView(layoutEmpty);

        }

        isRefresh = false;
        canLoad = false;

        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void setPresenter(PublicityOfApplyResultSearchContract.Presenter presenter) {

    }

    @Override
    public void showLoading() {
        showProgressDialog(false, null);
    }

    @Override
    public void dismissLoading() {
        dismissProgressDialog();
    }
}
